package com.zelin.seckill.service.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zelin.dao.SeckillGoodsDao;
import com.zelin.entity.SeckillGoodsEntity;
import com.zelin.utils.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * ------------------------------
 * 功能：定时向redis中添加商品
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/18-16:23
 * ------------------------------
 */
@Component
@EnableScheduling
public class SeckillGoodsPushTask {
    @Autowired
    private SeckillGoodsDao seckillGoodsDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 功能: 将数据库中的数据加载到redis中
     * 参数:
     * 返回值: void
     * 时间: 2021/8/18 16:24
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void loadDataToRedis(){

        //1. 得到日期菜单
        List<Date> dateMenus = MyDate.getDateMenus();
        //2. 遍历日期菜单
        for (Date dateMenu : dateMenus) {
            //2.1 定义查询条件
            QueryWrapper<SeckillGoodsEntity> queryWrapper = new QueryWrapper<SeckillGoodsEntity>()
                                            .eq("status", 1)
                                            .ge("start_time",dateMenu)
                                            // .lt("start_time",MyDate.addDateHour(dateMenu,2))
                                            .lt("end_time",MyDate.addDateHour(dateMenu,2))
                                            .gt("stock_count",0);
            //2.2 查询redis中指定大key中是否包含有小key集合
            Set keys = redisTemplate.boundHashOps("SeckillGoods_" + MyDate.getDateStr(dateMenu)).keys();
            //2.3 判断是否keys存在
            if(keys != null && keys.size() > 0){
                queryWrapper.notIn("id",keys);
            }
            //2.4 查询商品列表
            List<SeckillGoodsEntity> goodsEntities = seckillGoodsDao.selectList(queryWrapper);
            //2.5 将商品放到redis中
            for (SeckillGoodsEntity goodsEntity : goodsEntities) {
                BoundHashOperations<String, String, String> hashOperations = redisTemplate.boundHashOps("SeckillGoods_" + MyDate.getDateStr(dateMenu));
                hashOperations.put(goodsEntity.getId() + "", JSON.toJSONString(goodsEntity));
                //设置过期时间

                //以SeckillGoodsCountList_商品id为key，将一个指定的数组(数组的长度为商品数量，元素是商品id)放到redis中
                String[] ids = pushGoods(goodsEntity.getStockCount(), goodsEntity.getId());
                redisTemplate.boundListOps("SeckillGoodsCountList_"+goodsEntity.getId()).leftPushAll(ids);

                //以SeckillGoodsCount为大key，以商品id为小key，作一个对应商品的自增键，以后下单扣完库存的实际数量，就是这里的自增键
                redisTemplate.boundHashOps("SeckillGoodsCount").increment(goodsEntity.getId()+"",goodsEntity.getStockCount());
            }
        }
    }

    /**
     * 功能: 以商品数量为数组长度，以商品id为数组元素
     * 参数:
     * 返回值:
     * 时间: 2021/8/20 15:36
     * [1001,1001,1001,1001,1001]
     */
    private String[] pushGoods(int len,long id){
        String[] ids = new String[len];
        for (int i = 0; i < len; i++) {
            ids[i] = id + "";
        }
        return ids;
    }
}
